package util;

import model.AdminUser;
import util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Question AdminDao
 * @Date 2022/12/16 22:30
 * @Solution
 */
public class AdminDao {
    public AdminUser selectByName(String adminName) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection=DBUtil.getConnection();
            String sql = "select * from admin_user where admin_name<=>?";
            statement=connection.prepareStatement(sql);
            statement.setString(1,adminName);
            resultSet = statement.executeQuery();
            if(resultSet.next()) {
                AdminUser admin = new AdminUser();
                admin.setId(resultSet.getInt("id"));
                admin.setUsername(resultSet.getString("admin_name"));
                admin.setPassword(resultSet.getString("password"));
                return admin;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }
}
